#include "ad5242.h"
#include "twi.h"

uint8_t read_ad5242(uint8_t channel)
{


	if(channel == 0) pot_status = 0b01111111 & pot_status;
	else pot_status = 0b10000000 | pot_status;

	twi_buf[0] = pot_status;

	twi_flags.transmit_end=0;
	twi_start(0b01011000,TX_TWI,1);	
	while (twi_flags.transmit_end!=1);


	twi_flags.receive_end = 0;
	twi_start(0b01011000,RX_TWI,1);
	while (twi_flags.receive_end!=1);

	
	return twi_buf_rx[0];
}

void set_ad5242(uint8_t channel, uint8_t vol)
{
	if(channel == 0) pot_status = 0b01111111 & pot_status;
	else pot_status = 0b10000000 | pot_status;

	twi_buf[0] = pot_status;

	twi_buf[1]=vol;

	twi_flags.transmit_end=0;
	twi_start(0b01011000,TX_TWI,2);	
	while (twi_flags.transmit_end!=1);
	
}
void sbi_O1_ad5242(void)
{
	pot_status = 0b00010000 | pot_status;

	twi_buf[0] = pot_status;

	twi_flags.transmit_end=0;
	twi_start(0b01011000,TX_TWI,1);	
	while (twi_flags.transmit_end!=1);
}

void sbi_O2_ad5242(void)
{
	pot_status = 0b00001000 | pot_status;

	twi_buf[0] = pot_status;

	twi_flags.transmit_end=0;
	twi_start(0b01011000,TX_TWI,1);	
	while (twi_flags.transmit_end!=1);
}

void cbi_O1_ad5242(void)
{
	pot_status = 0b11101111 & pot_status;

	twi_buf[0] = pot_status;

	twi_flags.transmit_end=0;
	twi_start(0b01011000,TX_TWI,1);	
	while (twi_flags.transmit_end!=1);
}

void cbi_O2_ad5242(void)
{
	pot_status = 0b11110111 & pot_status;

	twi_buf[0] = pot_status;


	twi_flags.transmit_end=0;
	twi_start(0b01011000,TX_TWI,1);	
	while (twi_flags.transmit_end!=1);
}
